<?php

class Empeval_Methods_Request_GetMenuData {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	
	private static $TO_EMPLOYEE_ACCEPT = array(
		EmpevalBonusGoalsForms::STATUS_TO_EMPLOYEE_ACCEPT_1,
		EmpevalBonusGoalsForms::STATUS_TO_USER_ACCEPT_2,
		EmpevalBonusGoalsForms::STATUS_TO_USER_ACCEPT_3
	);
	
	private static $EMPLOYEE_REJECTED = array(
			EmpevalBonusGoalsForms::STATUS_NEW,
			EmpevalBonusGoalsForms::STATUS_WAITING_FOR_SEND_TO_ACCEPT
	);
	
	private static $TO_SUPERIOR_ACCEPT = array(
			EmpevalBonusGoalsForms::STATUS_TO_SUPERIOR_ACCEPT_1,
			EmpevalBonusGoalsForms::STATUS_TO_SUPERIOR_ACCEPT_2
	);
	
	private static $SUPERIOR_REJECTED = array(
			EmpevalBonusGoalsForms::STATUS_NEW,
			EmpevalBonusGoalsForms::STATUS_WAITING_FOR_SEND_TO_ACCEPT
	);
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
	}
	
	public function process() {
		if (!Empeval_Auth::getInstance()->isAuth())
			return new Empeval_Methods_Response_GetMenuData(false);
		
		$response = new Empeval_Methods_Response_GetMenuData(true);
		
		$response->employeeAcceptCount = EmpevalBonusGoalsFormsQuery::create()
			->useUserPositionsQuery()
				->filterByUsers(Empeval_Auth::getInstance()->getUser())
			->endUse()
			->filterByStatus(self::$TO_EMPLOYEE_ACCEPT)
			->count();
		
		$response->employeeRejectedCount = EmpevalBonusGoalsFormsQuery::create()
			->useUserPositionsQuery()
				->filterByUsers(Empeval_Auth::getInstance()->getUser())
			->endUse()
			->filterByNoAcceptReason(null, Criteria::ISNOTNULL)
			->filterByStatus(self::$EMPLOYEE_REJECTED)
			->count();
		
		$userPosition = Empeval_Auth::getInstance()->getUser()->getUserPositionss()->getFirst();
		if ($userPosition instanceof EmpevalUserPositions && $userPosition->getIsSuperior()) {
			$childUnits = array();
			foreach ($userPosition->getUnits()->getUnitssRelatedById() as $unit) {
				$childUnits[] = $unit->getId();
			}
			$subordinatesCount = EmpevalUserPositionsQuery::create()
				->addSuperiorUserPositionFilters($userPosition->getUnitId(), $childUnits)
				->count();
			$response->hasSubordinates = ($subordinatesCount > 0);
			
			$response->superiorAcceptCount = EmpevalBonusGoalsFormsQuery::create()
			->useUserPositionsQuery()
				->addSuperiorUserPositionFilters($userPosition->getUnitId(), $childUnits)
			->endUse()
			->filterByStatus(self::$TO_SUPERIOR_ACCEPT)
			->count();
			
			$response->superiorRejectedCount = EmpevalBonusGoalsFormsQuery::create()
			->useUserPositionsQuery()
				->addSuperiorUserPositionFilters($userPosition->getUnitId(), $childUnits)
			->endUse()
			->filterByNoAcceptReason(null, Criteria::ISNOTNULL)
			->filterByStatus(self::$SUPERIOR_REJECTED)
			->count();
		}
		else {
			$response->hasSubordinates = false;
		}
		
		$rejections = null;
		if (Empeval_Auth::getInstance()->getUser()->isAdmin() || Empeval_Auth::getInstance()->getUser()->isHr())
			$response->rejectionsCount = EmpevalRejectionsQuery::create()->filterByStatus(0)->count();
		
		return $response;
	}
	
}